package com.cskaoyan.mall.dao.implement;

import com.cskaoyan.mall.dao.UserDao;
import com.cskaoyan.mall.model.bo.AdminAddBO;
import com.cskaoyan.mall.model.bo.AdminLoginBO;
import com.cskaoyan.mall.model.sql.User;
import com.cskaoyan.mall.model.vo.AdminLoginVO;
import com.cskaoyan.mall.model.vo.OrderByStateVO;
import com.cskaoyan.mall.model.vo.TempGoodVO;
import com.cskaoyan.mall.model.vo.UserSignUpVO;
import com.cskaoyan.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());

    @Override
    public List<User> allUser(HttpServletRequest request, HttpServletResponse response) {
        List<User> query = null;
        try {
            query = runner.query("select * from user ", new BeanListHandler<User>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public void deleteUser(String id) {
        try {
            int update = runner.update("delete from user where id = ?", id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public List<User> searchUser(String word) {
        List<User> userList = null;
        try {
            userList = runner.query("select * from user where nickname like ?",
                    new BeanListHandler<>(User.class),
                    "%"+word+"%");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(userList==null){
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return userList;
    }

    @Override
    public UserSignUpVO signup(User user) {
        //不能有重复名字
        //未实现...
        UserSignUpVO userSignUpVO = new UserSignUpVO();
        try {
            int update = runner.update("insert into user(email,nickname,pwd,recipient,address,phone) " +
                            "value(?,?,?,?,?,?)", user.getEmail(), user.getNickname(), user.getPwd(),
                    user.getRecipient(), user.getAddress(), user.getPhone());
            if(update!=0){
                userSignUpVO.setCode(0);
                userSignUpVO.setName(user.getNickname());
                userSignUpVO.setToken(user.getNickname());
                }else {
                userSignUpVO.setCode(10000);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userSignUpVO;
    }

    /**
     * CheckEmail  and CheckPwd
     * @param userLogin
     * @return
     */
    @Override
    public String login(AdminLoginBO userLogin) {
        AdminAddBO vo = new AdminAddBO();
        try {
            vo = runner.query("select nickname from user where email = ? and pwd = ?",
                    new BeanHandler<>(AdminAddBO.class),
                    userLogin.getEmail(),
                    userLogin.getPwd());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(vo.getNickname()==""||vo.getNickname()==null){return null;}
        return vo.getNickname();
    }

    //额外计算 goodsNum ,price --->unitPrice  ---->GoodsName  img
    @Override
    public List<OrderByStateVO> getOrderByState(String state, String token) {
        List<OrderByStateVO> list = new ArrayList<>();
        String sql = "select id,num,stateId,amount,goodsDetailId,createtime,hasComment," +
                "goodsId,spec from orders where 1 = 1";
        if("-1".equals(state)){
            sql+=" and name = ?";
        }else {
            sql+=" and stateId = "+state+" and name = ?";
        }
        try {
            list = runner.query(sql, new BeanListHandler<>(OrderByStateVO.class), token);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //此时，还有  good{   img=null , name = null ; unitPrice =0;        }

        //unitPrice = goodsNum * amount
        for (OrderByStateVO o:list
             ) {
            int goodsid = (int)o.getGoods().getId();
            TempGoodVO tempGoodVO1 = null;
            try {
                tempGoodVO1 = runner.query("select name,img from goods where id = ?", new BeanHandler<>(TempGoodVO.class), goodsid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            o.getGoods().setName(tempGoodVO1.getName());
            o.getGoods().setImg(tempGoodVO1.getImg());
            double unitPrice = (double) (o.getGoodsNum()*o.getAmount());
            o.getGoods().setUnitPrice(unitPrice);

            Double amount = o.getAmount();
            o.setAmount(o.getGoods().getUnitPrice());
            o.getGoods().setUnitPrice(amount);
        }

        return list;
    }
}
